<?php

include_once 'eventmodel.php';
include_once 'util.php';

$page = new pager();

$list_type_event = get_loai_sukien();

$list_sukien = get_all_event($page, "?page=");

$list_type = get_all_event_type($page, "?page=");

if ($_POST["checksubmit"] == "submitsk") {
    $date = date("Y-m-d H:i:s", strtotime("{$_POST["ngaylapsukien"]}"));
    $array = array(
        "id" => "''",
        "user_id" => "{$_COOKIE["id"]}",
        "tensukien" => "'{$_POST["tensukien"]}'",
        "ngaylapsukien" => "'{$date}'",
        "event_type_id" => "{$_POST["event_type_id"]}",
        "diadiem" => "'{$_POST["diadiem"]}'",
        "noidung" => "'{$_POST["noidung"]}'",
    );
    them_moi_event($array);
    //echo "<script>window.location ='sukien.php';</script>";
}

if ($_POST["checkSubmit"] == "submittype") {

    if ((!empty($_FILES["hinhanh"])) && ($_FILES['hinhanh']['error'] == 0)) {

        $arr_images = getFileInDirectory("../../data/event/");
        $filename = strtolower(basename($_FILES['hinhanh']['name']));
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (!empty($arr_images)) {
            foreach ($arr_images as $file) {
                if ($file == $_FILES["hinhanh"]["name"]) {
                    $name = $file . "_" . md5(time()) . "." . $ext;
                    $folder = '../../data/event/' . $name;
                    break;
                } else {
                    $name = $_FILES['hinhanh']['name'];
                    $folder = '../../data/event/' . $_FILES['hinhanh']['name'];
                }
            }
            move_uploaded_file($_FILES['hinhanh']['tmp_name'], $folder);
        } else {
            $name = $_FILES['hinhanh']['name'];
            $folder = '../../data/event/' . $_FILES['hinhanh']['name'];
            move_uploaded_file($_FILES['hinhanh']['tmp_name'], $folder);
        }
    } else {
        $name = "default.png";
    }
    $array = array(
        "id" => "''",        
        "loaisukien" => "'{$_POST["loaisukien"]}'",
        "image" => "'{$name}'"       
    );
    them_moi_eventtype($array);
}
if($_GET["action"] == "suaevent"){
    echo get_sukien_type_byid($_GET["id"]);
}
if($_POST["checkSubmit"] == "updatetype"){
    if ((!empty($_FILES["hinhanh"])) && ($_FILES['hinhanh']['error'] == 0)) {

        $arr_images = getFileInDirectory("../../data/event/");
        $filename = strtolower(basename($_FILES['hinhanh']['name']));
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (!empty($arr_images)) {
            foreach ($arr_images as $file) {
                if ($file == $_FILES["hinhanh"]["name"]) {
                    $name = $file . "_" . md5(time()) . "." . $ext;
                    $folder = '../../data/event/' . $name;
                    break;
                } else {
                    $name = $_FILES['hinhanh']['name'];
                    $folder = '../../data/event/' . $_FILES['hinhanh']['name'];
                }
            }
            move_uploaded_file($_FILES['hinhanh']['tmp_name'], $folder);
        } else {
            $name = $_FILES['hinhanh']['name'];
            $folder = '../../data/event/' . $_FILES['hinhanh']['name'];
            move_uploaded_file($_FILES['hinhanh']['tmp_name'], $folder);
        }
    } else {
        $name = $_POST["gethinhanh"];
    }
    $array = array(             
        "loaisukien" => "'{$_POST["loaisukien"]}'",
        "image" => "'{$name}'"       
    );        
    sua_eventtype($array,$_POST["data_id"]);
}
if($_GET["action"] == "xoa"){
    echo xoa_eventtype($_POST["id"]);
}
?>
